package com.itheima.ydd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.R;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.YddAdvposDto;
import com.itheima.ydd.entity.YddAdvimg;
import com.itheima.ydd.entity.YddAdvpos;
import com.itheima.ydd.service.IYddAdvimgService;
import com.itheima.ydd.service.IYddAdvposService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@Slf4j
@RestController
@RequestMapping("/ydd_advpos")
public class YddAdvposController {

    @Autowired
    private IYddAdvposService yddAdvposService;

    @Autowired
    private IYddAdvimgService yddAdvimgService;


    /**
     * 分页查询&模糊查询
     *
     * @param advposdesc
     * @param advposname
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list")
    public ResultData list(String advposdesc, String advposname, int page, int limit) {
        log.info("查询参数{}", advposdesc, advposname);
        //构造分页构造器对象
        Page<YddAdvpos> pageInfo = new Page<>(page, limit);
        Page<YddAdvposDto> pageDto = new Page<>();
        //条件构造器
        LambdaQueryWrapper<YddAdvpos> queryWrapper = new LambdaQueryWrapper<>();
        //添加过滤条件
        queryWrapper.like(StringUtils.isNotEmpty(advposdesc), YddAdvpos::getAdvposdesc, advposdesc);
        queryWrapper.like(StringUtils.isNotEmpty(advposname), YddAdvpos::getAdvposname, advposname);
        //执行分页查询
        yddAdvposService.page(pageInfo, queryWrapper);

        List<YddAdvpos> records = pageInfo.getRecords();
        List<YddAdvposDto> list = records.stream().map((item) -> {
            YddAdvposDto dto = new YddAdvposDto();
            BeanUtils.copyProperties(item, dto);
            //查询广告图数量
            LambdaQueryWrapper<YddAdvimg> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(item.getId() != null, YddAdvimg::getAdvimgpos, item.getId());
            int count = yddAdvimgService.count(wrapper);
            dto.setAdvposnum(count);
            return dto;
        }).collect(Collectors.toList());
        pageDto.setRecords(list);

        HashMap<String, Object> map = new HashMap<>();
        map.put("count",pageInfo.getTotal());
        map.put("totalPages",pageInfo.getPages());
        map.put("pageSize",pageInfo.getSize());
        map.put("currentPage",pageInfo.getCurrent());
        map.put("data",pageDto.getRecords());
        return ResultData.ok("",map);

    }

    /**
     * 新增广告位信息
     * @param yddAdvpos
     * @return
     */
    @PostMapping("/add")
    public R save(@RequestBody YddAdvpos yddAdvpos){
        log.info("接收参数为：yddAdvpos:{}", yddAdvpos);
        boolean save = yddAdvposService.save(yddAdvpos);
        if (save){
            return R.success("添加成功","");
        }else {
            return R.error("凉凉了老铁~~快去瞅瞅什么问题!");
        }

    }

    /**
     * 修改位时回显数据
     * @param id
     * @return
     */
    @GetMapping("rowInfo")
    public ResultData get(Integer id){
        //调用getById方法获取该id所对应的广告位信息对象
        YddAdvpos advpos = yddAdvposService.getById(id);
        if (advpos != null){
            //不为空，返回该id所对应的广告位信息
            return ResultData.ok("",advpos);
        }else {
            //为空时返回错误提示信息
            return ResultData.error("没有广告信息");
        }
    }

    /**
     * 修改广告位信息
     * @param yddAdvpos
     * @return
     */
    @PostMapping("/update")
    public R update(@RequestBody YddAdvpos yddAdvpos){
        if (yddAdvpos != null){
            yddAdvposService.updateById(yddAdvpos);
            return R.success("","修改成功");
        }else {
            return R.error("这是空的啊，改你老木啊!");
        }
    }

    /**
     * 删除广告位信息
     * @param id
     * @return
     */
    @GetMapping("/del")
    public R remove(Integer id){
        log.info("删除的id为: {}",id);
        YddAdvpos yddAdvpos = yddAdvposService.getById(id);
        //判断是否为默认数据,如果是，则不能删除
        String defaultData = yddAdvpos.getDefaultData();
        if (defaultData != null && defaultData.equals("1")){
            return R.error("默认数据,不能删啊吊毛!");
        }

        //判断广告图数量，如果不为0，也不能删除
        LambdaQueryWrapper<YddAdvimg> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(yddAdvpos.getId() != null,YddAdvimg::getAdvimgpos,id);
        int count = yddAdvimgService.count(queryWrapper);
        if (count != 0){
            return R.error("广告图数量不为0不能删除");
        }else {
            yddAdvposService.removeById(id);
            return R.success("删除成功","");
        }
    }

    /**
     * 广告图位置下拉列表
     * @return
     */
    @GetMapping("all")
    public ResultData all() {
        List<YddAdvpos> list = yddAdvposService.list();
        //R r = new R();
        //r.putData("data",list);
        //r.setErrno(0);
        return ResultData.ok("",list);
    }


}
